<?php
namespace Admin\Controller;
use Common\Model\OptionModel;
class FuturesQqlcdController extends AdminController{
	//页面加载和搜索
	public function index(){
		$Model = new OptionModel("order");//实例化MongoDB
//		ini_set('mongo.long_as_object',1);
		/*状态查询*/
		if($_REQUEST['status']){
			if($_REQUEST['status']==1){
				$sql['status']=1;
				$status=1;
			}elseif($_REQUEST['status']==2){
				$sql['status']=2;
				$status=2;
			}else{
				$status=-1;
			}
		}else{
			$status=-1;
		}
		//方向查询
		if($_REQUEST['direction']){
			if($_REQUEST['direction']==1){
				$sql['direction']="1";
				$direction=1;
			}elseif($_REQUEST['direction']==2){
				$sql['direction']="0";
				$direction=2;
			}else{
				$direction=-1;
			}
		}
		else{
			$status=-1;
		}
		/*合约查询*/

		if($_REQUEST['option']){
			$map['name']=array('like',"%".trim($_REQUEST['option'])."%");
			$option = M('Futures_option')->where($map)->field('id')->select();
			foreach($option as $k=>$v){
				$arr[$k] = $v['id'];
			}
			$sql['option_id'] = array('in',$arr);
		}
		//开始时间查询
		$start_time=I('get.start_time');
		if($start_time){
			$start_time=strtotime($start_time);
			$sql['start_time']=array('gte',$start_time);
			$this->assign('start_time',$start_time);
		}
		$end_time=I('get.end_time');
		if($end_time){
			$end_time=strtotime($end_time)+86400;
			$sql['start_time']=array('lte',$end_time);
			$this->assign('end_time',$end_time);
		}
		if($start_time && $end_time){
			$sql['start_time']=array('$gte'=>$start_time,'$lte'=>$end_time);
		}
		/*用户查询*/
		if($_REQUEST['name']){
			$map['nickname']=array('like',"%".trim($_REQUEST['name'])."%");
			$nickname = M('follow')->where($map)->field('id')->select();
			$routes=array();
			foreach($nickname as $k=>$v){
				$routes[$k] = $v['id'];
			}
			$sql['user_id'] = array('in',$routes);
		}
		//分页
		$count = $Model->where($sql)->count();
		$Page       = new \Think\Page($count,20);
		$show       = $Page->show();// 分页显示输出
		//查询订单数据
		$list = $Model->where($sql)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		//获取用户名和合约名
		foreach($list as $k=>$v){
				$user = M('follow')->where('id='.$v['user_id'])->field('nickname')->find();
				$option = M('Futures_option')->where('id='.$v['option_id'])->field('name,code')->find();
				$list[$k]['user'] = $user['nickname'];
				$list[$k]['option'] = $option['name'];
				$list[$k]['code'] = $option['code'];
		}
		if(!$_REQUEST['name']){
			$mkarr['maker_id']=array('eq',0);
			$makeId=M('follow')->where($mkarr)->select();
			$mId=array();
			foreach($makeId as $mk){
				$mId[]=$mk['id'];
			}
			$sql['user_id']=array('nin',$mId);
		}
		$showdata=$Model->where($sql)->select();
		$topmoney="";
		$ying="";
		$kui="";
		foreach($showdata as $v){
			$topmoney+=$v['invest'];
			if($v['profit']>0){
			$ying+=$v['profit']-$v['invest'];
			}else{
				$kui+=$v['profit'];
			}
		}
		$topmoney = round($topmoney,2);
		$ying = round($ying,2);
		$this->assign('topmoney',$topmoney);
		$this->assign('ying',$ying);
		$this->assign('kui',$kui);
		$this->assign('direction',$direction);
		$this->assign('status',$status);
		$this->assign('list',$list);
		$this->assign('_page',$show);
		$this->display();
	}
	
	//删除
	public function del(){
		$Model = new OptionModel('order');		//实例化MongoDB
		if(IS_POST){
			$ids = $_POST['ids'];
			foreach($ids as $v){
				$id= $v;
				$where = array('id'=>(int)$id);	
				$data = $Model->where($where)->delete();
			}
			$this->success('删除成功',U('FuturesQqlcd/index'));
		}
	}
	
	//导出
	public function downexecl(){
		$Model = new OptionModel('order');		//实例化MongoDB
		/*状态查询*/
		if($_REQUEST['status']){
			if($_REQUEST['status']==1){
				$sql=array('status'=>1);
				$status=1;
			}elseif($_REQUEST['status']==2){
				$sql=array('status'=>0);
				$status=2;
			}else{
				$status=-1;
			}
		}
		else{
			$status=-1;
		}
		//方向查询
		if($_REQUEST['direction']){
			if($_REQUEST['direction']==1){
				$sql=array('direction'=>1);
				$direction=1;
			}elseif($_REQUEST['direction']==2){
				$sql=array('direction'=>'0');
				$direction=2;
			}elseif($_REQUEST['direction']==3){
				$sql=array('direction'=>'2');
				$direction=3;
			}else{
				$direction=-1;
			}
		}
		else{
			$status=-1;
		}
		/*用户查询*/
		if($_GET['name']){
			$map['nickname']=array('like',"%".trim($_GET['name'])."%");
			$nickname = M('follow')->where($map)->field('id')->select();
			foreach($nickname as $k=>$v){
				$routes[$k] = $v['id'];
			}
			$sql = array('user_id'=>array('in',$routes));
		}
		/*合约查询*/
		if($_GET['option']){
			$map['name']=array('like',"%".trim($_GET['option'])."%");
			$option = M('Futures_option')->where($map)->field('id')->select();
			foreach($option as $k=>$v){
				$arr[$k] = $v['id'];
			}
			$sql = array('option_id'=>array('in',$arr));
		}
		//开始时间查询
		$start_time=I('get.start_time');
		if($start_time){
			$start_time=strtotime($start_time);
			$sql['start_time']=array('gte',$start_time);
			$this->assign('start_time',$start_time);
		}
		$end_time=I('get.end_time');
		if($end_time){
			$end_time=strtotime($end_time)+86400;
			$sql['start_time']=array('lte',$end_time);
			$this->assign('end_time',$end_time);
		}
		if($start_time && $end_time){
			$sql['start_time']=array('$gte'=>$start_time,'$lte'=>$end_time);
		}
		$list = $Model->where($sql)->order('id desc')
		->field('user_id,option_id,invest,buy_price,sell_price,direction,profit,start_time,duration,yield_rate,end_time,status,id')->select();
		$filename='恒指期权订单.csv';				//设置文件名称
		header("Content-Disposition:attachment;filename=".$filename);		//保存文件，文件名为$filename
		header("Content-type:text/csv");		//内容类别
		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');		//缓存控制
		header('Expires:0');		//期限，可以用于设定网页的到期时间。一旦网页过期，必须到服务器上重新传输 
		header('Pragma:public');	//用于设定禁止浏览器从本地机的缓存中调阅页面内容 
		//要导出的数据设置
		$data .= "用户,";
		$data .= "合约,";
		$data .= "金额,";
		$data .= "当前卖价,";
		$data .= "平仓买价,";
		$data .= "方向,";
		$data .= "盈利,";
		$data .= "开始时间,";
		$data .= "时长,";
		$data .= "收益率,";
		$data .= "结束时间,";
		$data .= "状态,";
		$data .= "id,";
		$data .= "合约代码,\n";
		//修改字段
		foreach($list as $k => $v){
			$user = M('follow')->where('id='.$v[user_id])->field('nickname')->find();
			$option = M('Futures_option')->where('id='.$v[option_id])->field('name,code')->find();
			if($v['direction'] == 1){
				$list[$k]['direction'] = '看涨';
			}elseif($v['direction'] == 0){
				$list[$k]['direction'] = '看跌';
			}elseif($v['direction'] == 2){
				$list[$k]['direction'] = '看平';
			}
			if($v['status'] == 1){
				$list[$k]['status'] = '进行中';
			}elseif($v['status'] == 2){
				$list[$k]['status'] = '已结束';
			}
			$list[$k]['start_time'] = date("Y/m/d",$v['start_time']);
			$list[$k]['end_time'] = date("Y/m/d",$v['start_time']);
			$list[$k]['user_id'] = $user['nickname'];
			$list[$k]['option_id'] = $option['name'];
			$list[$k]['code'] = $option['code'];
		}
		//编辑需导出的数据
		foreach($list as $v){
			foreach($v as $key => $value){
				$va = str_replace(',','，',$value);
				if($key == "code"){
					$vo = $va.",\n";
				}else{
					$vo = $va.",";
				}
				if($key != "_id"){
					$data .= $vo;
				}
			}
			
		}
		echo  @mb_convert_encoding($data, 'GB2312','utf-8');
	}
	public function search_string(){
		$status=I('get.status');
		if($status && $status!=-1){
			$where['status']=intval($status);
			$this->assign('status',$status);
		}
		$direction=I('get.direction');
		if($direction && $direction!=-1){
			$where['direction']=$direction;
			$this->assign('direction',$direction);
		}
		$name=I('get.name');
		if($name){
			$user['nickname']=array('like','%'.$name.'%');
			$userdata=M('follow')->where($user)->select();
			$arr=array();
			foreach($userdata as $v){
				$arr[]=$v['id'];
			}
			$where['user_id']=array('in',$arr);
			$this->assign('name',$name);
		}
		$option=I('get.option');
		if($option){
			$optionName['name']=array('like','%'.$option.'%');
			$userdata=M('futures_option')->where($optionName)->select();
			$optionarr=array();
			foreach($userdata as $value){
				$optionarr[]=$value['id'];
			}
			$where['option_id']=array('in',$optionarr);
			$this->assign('option',$option);
		}
		$start_time=I('get.start_time');
		if($start_time){
			$start_time=strtotime($start_time);
			$where['start_time']=array('gte',$start_time);
			$this->assign('start_time',$start_time);
		}
		$end_time=I('get.end_time');
		if($end_time){
			$end_time=strtotime($end_time)+86400;
			$where['start_time']=array('lte',$end_time);
			$this->assign('end_time',$end_time);
		}
		if($start_time && $end_time){
			$where['start_time']=array('$gte'=>$start_time,'$lte'=>$end_time);
		}
		return $where;
	}
	public function ouyuan(){
//		ini_set('mongo.long_as_object',1);
		$where=$this->search_string();
		$Model=new OptionModel('euro_order');
		$size=$Model->where($where)->count();
		$page=new \Think\Page($size,20);
		$odata=$Model->where($where)->limit($page->firstRow.','.$page->listRows)->order('_id desc')->select();
		foreach($odata as $key=>$val){
			$username=M('follow')->where('id='.$val['user_id'])->find();
			$opname=M('futures_option')->where('id='.$val['option_id'])->find();
			$odata[$key]['opname']=$opname['name'];
			$odata[$key]['code']=$opname['code'];
			$odata[$key]['user_name']=$username['nickname'];
		}
		$mkarr['maker_id']=array('eq',0);
		$makeId=M('follow')->where($mkarr)->select();
		$mId=array();
		foreach($makeId as $mk){
			$mId[]=$mk['id'];
		}
		$where['user_id']=array('nin',$mId);
		$name=I('get.name');
		if($name){
			$user['nickname']=array('like','%'.$name.'%');
			$userdata=M('follow')->where($user)->select();
			$arr=array();
			foreach($userdata as $v){
				$arr[]=$v['id'];
			}
			$where['user_id']=array('in',$arr);
			$this->assign('name',$name);
		}
		$showdata=$Model->where($where)->select();
		$topmoney="";
		$ying="";
		$kui="";
		foreach($showdata as $v){
			$topmoney+=$v['invest'];
			if($v['profit']>0){
			$ying+=$v['profit']-$v['invest'];
			}else{
			$kui+=$v['profit'];
			}
		}
		$topmoney = round($topmoney,2);
		$ying = round($ying,2);
		$this->assign('topmoney',$topmoney);
		$this->assign('ying',$ying);
		$this->assign('kui',$kui);
		$this->assign('odata',$odata);
		$this->assign('page',$page);
		$this->display();
	}

	public function delouyuan(){
		$Model = new OptionModel('euro_order');		//实例化MongoDB
		if(IS_POST){
			$ids = $_POST['ids'];
			foreach($ids as $v){
				$id= $v;
				$where = array('id'=>(int)$id);
				$data = $Model->where($where)->delete();
			}
			$this->success('删除成功',U('FuturesQqlcd/ouyuan'));
		}
	}

	public function down_ouyuan(){
		$where=$this->search_string();
		Vendor('PHPExcel');
		Vendor('Autoloader');
		$Model=new OptionModel('euro_order');
		$odata=$Model->where($where)->select();
		/*实例化phpexecl类*/
		$objPHPExcel = new \PHPExcel();
//		$objPHPExcel->getActiveSheet()->getStyle('C')->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
		$objPHPExcel->setActiveSheetIndex(0)
				//Excel的第一列的名称
				->setCellValue('A1', 'ID')
				->setCellValue('B1', '姓名')
				->setCellValue('C1', '合约')
				->setCellValue('D1', '合约代码')
				->setCellValue('E1', '金额')
				->setCellValue('F1', '当前卖价')
				->setCellValue('G1', '平仓买价')
				->setCellValue('H1', '方向')
				->setCellValue('I1', '盈利')
				->setCellValue('J1', '时长')
				->setCellValue('K1', '收益率')
				->setCellValue('L1', '状态')
				->setCellValue('M1', '开始时间')
				->setCellValue('N1', '结束时间');
		/*将数据放在phpexecl表中*/
		foreach($odata as $key=>$val){
			$username=M('follow')->where('id='.$val['user_id'])->find();
			$opname=M('futures_option')->where('id='.$val['option_id'])->find();
			$odata[$key]['opname']=$opname['name'];
			$odata[$key]['code']=$opname['code'];
			$odata[$key]['user_name']=$username['nickname'];
		}
		$num=2;
		foreach ($odata as $k => $value) {
			$objPHPExcel->setActiveSheetIndex(0)
					//Excel的第A列，uid是你查出数组的键值，下面以此类推
					->setCellValue('A' . $num, $value['id'])
					->setCellValue('B' . $num, $value['user_name'])
					->setCellValue('C' . $num, $value['opname'])
					->setCellValue('D' . $num, $value['code'])
					->setCellValue('E' . $num, $value['invest'] )
					->setCellValue('F' . $num, $value['buy_price'])
					->setCellValue('G' . $num, $value['sell_price'])
					->setCellValue('H' . $num, $value['direction']==1?"看涨":"看跌")
					->setCellValue('I' . $num, $value['profit'])
					->setCellValue('J' . $num, $value['duration'])
					->setCellValue('K' . $num, $value['yield_rate'])
					->setCellValue('L' . $num, $value['status']==1?"进行中":"已结束")
					->setCellValue('M' . $num, date('Y-m-d H:i:s',$value['start_time']))
					->setCellValue('N' . $num, date('Y-m-d H:i:s',$value['end_time']));
			$num++;
		}

		/*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
		$objPHPExcel->getActiveSheet()->setTitle('佣金明细');
		$objPHPExcel->setActiveSheetIndex(0);
		$time = date("Y/m/d");
		$head = $time."佣金明细.xls";
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename='.$head.'');
		header('Cache-Control: max-age=0');
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;
	}

}
?>